<template>
  <div class="course-list">
    <el-card>
      <div slot="header">
        <span>课程管理</span>
        <el-form
          ref="form"
          :inline="true"
          :model="form"
          label-width="100px"
          style="margin-top:25px;"
        >
          <el-form-item label="课程名称" prop="courseName">
            <el-input v-model="form.courseName"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status">
              <el-option label="全部" value></el-option>
              <el-option label="上架" value="1"></el-option>
              <el-option label="下架" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button :disabled="loading" @click="onReset">重置</el-button>
            <el-button type="primary" :disabled="loading" @click="onSearch"
              >查询</el-button
            >
          </el-form-item>
          <el-form-item style="float: right;">
            <el-button
              type="primary"
              @click="$router.push({ name: 'course-create' })"
              icon="el-icon-plus"
              >添加课程</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        :data="courses"
        v-loading="loading"
        style="width: 100%; margin-bottom: 20px"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column
          prop="courseName"
          label="课程名称"
          width="120"
        ></el-table-column>
        <el-table-column prop="price" label="价格"></el-table-column>
        <el-table-column prop="sortNum" label="排序"></el-table-column>
        <el-table-column prop="status" label="上架状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="scope.row.isStatusLoading"
              @change="onStateChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="price" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              @click="
                $router.push({
                  name: 'course-edit',
                  params: {
                    courseId: scope.row.id
                  }
                })
              "
              >编辑</el-button
            >
            <el-button
              @click="
                $router.push({
                  name: 'course-section',
                  params: {
                    courseId: scope.row.id
                  }
                })
              "
              >内容管理</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="form.currentPage"
        :page-sizes="[5, 10, 20]"
        :page-size="form.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        :disabled="loading"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getQueryCourses, changeState } from '@/services/course'
import { Form } from 'element-ui'

export default Vue.extend({
  name: 'CourseList',
  data () {
    return {
      form: {
        currentPage: 1,
        pageSize: 10,
        courseName: '',
        status: ''
      },
      courses: [],
      totalCount: 0,
      loading: false
    }
  },
  created () {
    this.loadCourses()
  },
  methods: {
    async loadCourses () {
      this.loading = true
      const { data } = await getQueryCourses(this.form)
      data.data.records.forEach((item: any) => {
        item.isStatusLoading = false
      })
      this.courses = data.data.records
      this.totalCount = data.data.total
      this.loading = false
    },
    onSearch () {
      this.form.currentPage = 1
      this.loadCourses()
    },
    onReset () {
      (this.$refs.form as Form).resetFields()
      this.form.currentPage = 1
      this.loadCourses()
    },
    handleSizeChange (val: number) {
      this.form.currentPage = 1
      this.form.pageSize = val
      this.loadCourses()
    },
    handleCurrentChange (val: number) {
      // 请求获取对应页码的数据
      this.form.currentPage = val
      this.loadCourses()
    },
    async onStateChange (course: any) {
      course.isStatusLoading = true
      const { data } = await changeState({
        courseId: course.id,
        status: course.status
      })
      this.$message.success(`${course.status === 0 ? '下架' : '上架'}成功`)
      course.isStatusLoading = false
    }
  }
})
</script>

<style lang="scss" scoped></style>
